package api

import (
	"github.com/gin-gonic/gin"
	"miao-server/lib/http"
	"miao-server/service"
	"miao-server/structs/base"
	"miao-server/structs/dto"
)

var BaseApi = new(baseApi)

type baseApi struct {
}

func (api baseApi) ClearDB(c *gin.Context) {
	in := struct {
		UniqueCode string      `json:"uniqueCode"`
		Inst       dto.ClearDb `json:"inst"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.InstructServer.ClearDB(in.UniqueCode, in.Inst)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (api baseApi) GetPcLoad(c *gin.Context) {
	in := struct {
		UniqueCode string          `json:"uniqueCode"`
		Inst       dto.LoadInstDto `json:"inst"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	msg, err := service.InstructServer.GetPcLoad(in.UniqueCode, in.Inst)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, msg)
}

func (api baseApi) SendInstruct(c *gin.Context) {
	in := struct {
		base.PluginInstruct
		UniqueCode string `json:"uniqueCode"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		return
	}
	err := service.InstructServer.SendInstruct(in.UniqueCode, in.PluginInstruct)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (api baseApi) SendInstructR(c *gin.Context) {
	in := struct {
		base.PluginInstruct
		UniqueCode string `json:"uniqueCode"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		return
	}
	msg, err := service.InstructServer.SendInstructR(in.UniqueCode, in.PluginInstruct)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, msg)
}
func (api baseApi) SendNobodyInstruct(c *gin.Context) {
	in := struct {
		UniqueCode string `json:"uniqueCode"`
		InstType   string `json:"instType"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.InstructServer.SendNobodyInstruct(in.UniqueCode, in.InstType)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (api baseApi) StartGuarder(c *gin.Context) {
	in := struct {
		PluginInstruct base.PluginInstruct `json:"pluginInstruct"`
		UniqueCode     string              `json:"uniqueCode"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.InstructServer.StartGuarder(in.UniqueCode, in.PluginInstruct)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (api baseApi) GetPolicy(c *gin.Context) {
	in := struct {
		UniqueCode string `json:"uniqueCode"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	msg, err := service.InstructServer.GetPolicy(in.UniqueCode)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, msg)
}

func (api baseApi) SetLogLv(c *gin.Context) {
	in := struct {
		UniqueCode string `json:"uniqueCode"`
		Lv         string `json:"lv"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.InstructServer.SetLogLv(in.UniqueCode, in.Lv)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (api baseApi) UpdatePlugin(c *gin.Context) {
	in := struct {
		UniqueCode string `json:"uniqueCode"`
		Filename   string `json:"filename"`
		Uri        string `json:"uri"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.InstructServer.UpdatePlugin(in.UniqueCode, in.Filename, in.Uri)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (api baseApi) AbnormalOlineDeviceList(c *gin.Context) {
	li := service.InstructServer.AbnormalOlineDeviceList()
	http.Ok(c, li)
}
func (api baseApi) OlineDevice(c *gin.Context) {
	li := service.InstructServer.OlineDevice()
	http.Ok(c, li)
}
